package util;

import android.content.Context;
import android.webkit.WebSettings;
import android.webkit.WebView;

/**
 * Created by Administrator on 2017/7/24 0024.
 */

public class WebSetting {

    public WebSetting(WebView mWv, Context context,String content) {
        WebSetting(mWv, context);
        mWv.loadDataWithBaseURL(null, loadHtml("康爱斯", content), "text/html", "UTF-8", null);
    }
    public WebSetting(WebView mWv, Context context,String content,String url) {
        WebSetting(mWv, context);
        mWv.loadDataWithBaseURL(url, null, "text/html", "UTF-8", null);
    }

    private void WebSetting(WebView webview, Context context) {
        WebSettings websetting = webview.getSettings();   //webview的许多属性都是通过websetting进行设置的

        websetting.setJavaScriptEnabled(true);   //可进行js交互

        websetting.setDomStorageEnabled(true);   //支持文件存储

        websetting.setDatabaseEnabled(true);    //支持数据库存储

        String appCacheDir = context.getDir("cache", Context.MODE_PRIVATE).getPath();   //缓存存储路径

        websetting.setAppCachePath(appCacheDir);   //设置缓存文件的路径

        websetting.setAppCacheEnabled(true);   //支持缓存

        // 我们可以在有网的情况下将缓存模式改为websetting.setCacheMode(WebSettings.LOAD_DEFAULT);当没有网络时则设置为
//        websetting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        websetting.setCacheMode(WebSettings.LOAD_DEFAULT);
        websetting.setAllowFileAccess(true);  // 可使用文件/

        websetting.setSupportZoom(true);

        websetting.setBuiltInZoomControls(true);

        websetting.setDisplayZoomControls(false);     //放大缩小的图标不显示，true或者不设置则为显示

        websetting.setUseWideViewPort(true);      //若要网页根据屏幕显示则需设置此属性

        websetting.setLoadWithOverviewMode(true);
        websetting.setDefaultTextEncodingName("utf-8");
    }

    public String loadHtml(String title, String content) {
        StringBuilder html = new StringBuilder();
        html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
        html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
        html.append("<head>");
        html.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
        html.append("<meta http-equiv=\"delete-UA-Compatible\" content=\"IE=edge\">");
        html.append("<meta http-equiv=\"Cache-Control\" content=\"no-transform\">");
        html.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\">");
        html.append("<meta name=\"keywords\" content=\"\">");
        html.append("<title>" + title + "</title>");
        html.append("<style> img { width: 100%; }</style>");
        html.append("</head>");
        html.append("<body>" + content + "</body>");
        html.append("</html>");
        return html.toString();
    }
}
